//
//  Untitled.swift
//  Skeleton
//
//  Created by bin hu on 2025/7/4.
//

import Foundation
import QuartzCore
import UIKit

let defaultNumbers = 5

// MARK: - 骨架图层池 (性能优化)
class SkeletonLayerPool {
    private var inactiveLayers = [CALayer]()
    
    func dequeueLayer() -> CALayer {
        inactiveLayers.popLast() ?? createNewLayer()
    }
    
    func enqueueLayer(_ layer: CALayer) {
        layer.removeAllAnimations()
        inactiveLayers.append(layer)
    }
    
    private func createNewLayer() -> CALayer {
        let layer = CAGradientLayer()
        layer.masksToBounds = true
        layer.shouldRasterize = true
        layer.rasterizationScale = UIScreen.main.scale
        return layer
    }
}

// MARK: - 颜色扩展
//extension UIColor {
//    func withBrightnessDelta(_ delta: CGFloat) -> UIColor {
//        var hue: CGFloat = 0
//        var saturation: CGFloat = 0
//        var brightness: CGFloat = 0
//        var alpha: CGFloat = 0
//        
//        if getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha) {
//            let newBrightness = max(0, min(1, brightness + delta))
//            return UIColor(hue: hue, saturation: saturation, brightness: newBrightness, alpha: alpha)
//        }
//        
//        return self
//    }
//}

extension UIColor {
    func withBrightnessDelta(_ delta: CGFloat) -> UIColor {
        var hue: CGFloat = 0
        var saturation: CGFloat = 0
        var brightness: CGFloat = 0
        var alpha: CGFloat = 0
        
        getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha)
        brightness = max(0, min(1, brightness + delta))
        
        return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: alpha)
    }
    
    func withSaturationDelta(_ delta: CGFloat) -> UIColor {
        var hue: CGFloat = 0
        var saturation: CGFloat = 0
        var brightness: CGFloat = 0
        var alpha: CGFloat = 0
        
        getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha)
        saturation = max(0, min(1, saturation + delta))
        
        return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: alpha)
    }
}


